#include "oneeventframe.h"

oneEventFrame::oneEventFrame(QWidget *parent) : QWidget(parent)
{
}
oneEventFrame::oneEventFrame(QVBoxLayout *InWidgetLayout, QString ProjectPath, QString EventName, QString EventType, QList<CMethodsInfo *> *MethodList, int Index, QWidget *parent):QWidget(parent){
    this->inWidgetLayout=InWidgetLayout;
    this->mProjectPath = ProjectPath;
    this->EventName=EventName;
    this->mConversionMethods = MethodList;
    this->index=Index;
    this->EventType = EventType;
    this->initWidget();
   // this->setVisible(false);

}

void oneEventFrame::initWidget(){
    oneFrame=new QGroupBox(EventName);
    inWidgetLayout->addWidget(oneFrame,0,Qt::AlignTop);

    QGridLayout *oneLayout = new QGridLayout();
    oneFrame->setLayout(oneLayout);
    oneFrame->setVisible(false);

    //elements

    QLabel *ChooseSchemaLabel=new QLabel(tr("Choose DataSchema:"));
    InternalComboBox  = new QComboBox();
    for(int i=0;i<this->mConversionMethods->size();i++){
        CMethodsInfo *mMethods = mConversionMethods->at(i);
        for(int j=0;j<mMethods->getCount();j++){
            QString MethodPath = mMethods->getMethod(j);
            MethodList.append(QFileInfo(MethodPath).fileName());
        }
    }
    //add tootip
    QStandardItemModel *model = new QStandardItemModel();
            for(int i = 0; i < MethodList.size(); ++i){
                QStandardItem *item = new QStandardItem(MethodList.at(i));
//                QString line="no schema";
//                QFile file(mConversionMethods->at(i)+"\\"+QFileInfo(mConversionMethods->at(i)).fileName()+".xml");
//                     if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
//                        line="";
//                         QTextStream in(&file);
//                         while (!in.atEnd()) {
//                             line += in.readLine();
//                             line +="\n";
//                         }
//                     }
//                item->setToolTip(line);
                model->appendRow(item);
            }

     InternalComboBox->setModel(model);

    //inputFile
    QLabel *Label=new QLabel(tr("Enter relative position:"));
    FileRelativePath = new QLineEdit();

    oneLayout->addWidget(ChooseSchemaLabel,0,0,1,1,Qt::AlignLeft);
    oneLayout->addWidget(InternalComboBox,0,1,1,1,Qt::AlignCenter);
    oneLayout->addWidget(Label,1,0,1,1,Qt::AlignLeft);
    oneLayout->addWidget(FileRelativePath,1,1,1,1,Qt::AlignCenter);

    //初始化属性
    if(mConversionMethods->size()>0){
         this->MethodFoldPath=mConversionMethods->at(0)->getMethod(0);
          this->MethodName = QFileInfo(mConversionMethods->at(0)->getMethod(0)).fileName();
    }

    connect(InternalComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_InternalMethod_Changed(QString)));
}

void oneEventFrame::setProperties(QString MethodFilePath,QString DataRelativePath){
    if(MethodFilePath.isEmpty()||DataRelativePath.isEmpty()) return ;
    if(QFile(MethodFilePath).exists()){
        on_InternalMethod_Changed(QFileInfo(MethodFilePath).fileName());
        this->InternalComboBox->setCurrentText(QFileInfo(MethodFilePath).fileName());
    }
    FileRelativePath->setText(DataRelativePath);
}

QString oneEventFrame::getMethodName(){
    return this->MethodName;
}
QString oneEventFrame::getFileRelativePath(){
    if(this->FileRelativePath->text()==""){
        return "./";
    }
    return this->FileRelativePath->text();
}
QString oneEventFrame::getEventName(){
    return this->EventName;
}
QString oneEventFrame::getEventType(){
    return this->EventType;
}
QString oneEventFrame::getMethodFoldPath(){
     return this->MethodFoldPath;
}
void oneEventFrame::on_InternalMethod_Changed(QString internalMethodName){
    this->MethodName=internalMethodName;

    for(int i=0;i<mConversionMethods->size();i++){
        for(int j=0;j<mConversionMethods->at(i)->getCount();j++){
            if(MethodName==QFileInfo(mConversionMethods->at(i)->getMethod(j)).fileName()){
                 this->MethodFoldPath=mConversionMethods->at(i)->getMethod(j);
                break;
            }
        }
    }
}











